package com.example.seckill.Controller;

import com.example.seckill.Exception.ErrorEnum;
import com.example.seckill.VO.BedVO;
import com.example.seckill.common.CommonResult;
import com.example.seckill.mapper.BedMapper;
import com.example.seckill.mapper.BuildingMapper;
import com.example.seckill.mapper.OrderMapper;
import com.example.seckill.mapper.RoomMapper;
import com.example.seckill.pojo.Bed;
import com.example.seckill.pojo.Student_Bed_Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/bed")
public class BedController {
    @Autowired
    BedMapper bedMapper;
    
    @Autowired
    RoomMapper roomMapper;

    @Autowired
    OrderMapper orderMapper;
    
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    BuildingMapper buildingMapper;

    @GetMapping("/getAllBeds")
    public CommonResult getAllBeds() {
        List<Bed> list=bedMapper.getAllBeds();

        if(list.isEmpty()) return CommonResult.error(ErrorEnum.NO_BED_FOUND);
        List<BedVO> res=new ArrayList();
        for(Bed bed:list){
            BedVO bedVO=new BedVO();
            bedVO.setStatus(bed.getStatus());
            bedVO.setId(bed.getId());
            bedVO.setRoom_id(bed.getRoom_id());
            bedVO.setBed_number(bed.getBed_number());
            Integer bid=buildingMapper.getBuildingIDbyRID(bedVO.getRoom_id());
            bedVO.setBuilding_id(bid);
            res.add(bedVO);

        }
        return CommonResult.success(res);
    }
    @PostMapping("/updateBedStatus")
    public CommonResult updateBedStatus(@RequestBody BedVO bedRequest) {
        System.out.println("更新状态");
        System.out.println("改变的状态="+bedRequest.getStatus());
        if(bedRequest==null) return CommonResult.error(ErrorEnum.REQUEST_ILLEGAL);
        Long bed_id=bedRequest.getId();
        Integer status=bedRequest.getStatus();
        if(status==2){
            Student_Bed_Order order=orderMapper.findOderByBedID(bed_id);
            if(order==null) return CommonResult.error(ErrorEnum.NO_ORDER_FOUND);
        }
        Bed foundBed=bedMapper.getBedByBedID(bed_id);
        if(foundBed==null) return CommonResult.error(ErrorEnum.NO_BED_FOUND);
        Integer current_status=foundBed.getStatus();
        if(current_status==2)  return CommonResult.error(ErrorEnum.DISTRIBUTED_BED);
        if(current_status!=status)
        {  Bed bed=new Bed();
            bed.setStatus(status);
            System.out.println(bed.getStatus());
            bed.setId(bed_id);
            bedMapper.changeStatus(bed);
            Bed bed1=bedMapper.getBedByBedID(bed_id);
            System.out.println(bed1.getStatus());
            if(bed.getStatus()==status) {
                //TODO：修改房间库存
                String rid=foundBed.getRoom_id();
                ValueOperations valueOperations=redisTemplate.opsForValue();
                if(status!=1) {
                    roomMapper.StockMinusOne(rid);
                    valueOperations.decrement("RoomInventory:"+rid);
                }
                else {
                    roomMapper.StockPlusOne(rid);
                    valueOperations.increment("RoomInventory:"+rid);
                }
                return CommonResult.success(bed);
            }
        }

        return CommonResult.error(ErrorEnum.ERROR);

    }
}
